*****************************************************************************************************
************                     Replication Code (Version 1.0) For:                   **************
************ Political Alignment and the Allocation of Stock Market Resources in China **************
*****************************************************************************************************

*** This .do file reproduces: Table 5 and 6 in the main text.

clear
use Listed_Firm_Performance.dta

********************************
***** Data Pre-processing  *****
********************************
gen symbol = substr(id,1,6)
gen yearend = year1
merge m:1 symbol yearend using Firm_Connection.dta
drop if _merge == 2
drop _merge

replace firm_pc = 0 if firm_pc ==.
replace pc_low = 0 if pc_low ==.
replace pc_city = 0 if pc_city ==.
gen pc_county = (pc_low == 1 & pc_city == 0)
gen firm_nlpc = (firm_pc == 1 & pc_county == 0)
replace npc = firm_nlpc

** Create numeric dummies for province, city, industry, board, and sponsor
egen ind_code = group(industry_csrc)
egen pro_code = group(province)
egen city_code = group(city)
egen board_code = group(board)
egen sponsor_code = group(sponsor)

** Rescale and log transformation for pre-IPO firm characteristics
gen lgfs=ln(firmsize0 + 1)
gen lgcapital = ln(registered_capital/1000 + 1)
replace gdp=gdppc*pop if gdp==.
gen lggdp=ln(gdp + 1)
gen lggdppc=ln(gdppc + 1)
gen lgpop=ln(pop + 1)
gen linv = ln(investment + 1)
gen lgovrev = ln(govrev + 1)
gen lgovexp = ln(govexp + 1)
gen unerate = unemployment / (pop * 100)

** Transform Data Structure: retrieve firm performance in IPO year t; t-1, t-2, t-3; t+1, t+2, t+3
// ROA
gen roa_ipo=.
gen roa_pre1=.
gen roa_pre2=.
gen roa_pre3=.
gen roa_post1=.
gen roa_post2=.
gen roa_post3=.

forvalues i=2004/2016 {
  replace roa_ipo=roa`i' if year1==`i'
  replace roa_pre1=roa`i-1' if year1-1==`i'
  replace roa_pre2=roa`i-2' if year1-2==`i'
  replace roa_pre3=roa`i-3' if year1-3==`i'
  replace roa_post1=roa`i+1' if year1+1==`i'
  replace roa_post2=roa`i+2' if year1+2==`i'
  replace roa_post3=roa`i+3' if year1+3==`i'
  }
  
//ROE
gen roe_ipo=.
gen roe_pre1=.
gen roe_pre2=.
gen roe_pre3=.
gen roe_post1=.
gen roe_post2=.
gen roe_post3=.

forvalues i=2004/2016 {
  replace roe_ipo=roe`i' if year1==`i'
  replace roe_pre1=roe`i-1' if year1-1==`i'
  replace roe_pre2=roe`i-2' if year1-2==`i'
  replace roe_pre3=roe`i-3' if year1-3==`i'
  replace roe_post1=roe`i+1' if year1+1==`i'
  replace roe_post2=roe`i+2' if year1+2==`i'
  replace roe_post3=roe`i+3' if year1+3==`i'
  }
  
// Revenue (scaled)
gen rev_ipo=.
gen rev_pre1=.
gen rev_pre2=.
gen rev_pre3=.
gen rev_post1=.
gen rev_post2=.
gen rev_post3=.

forvalues i=2004/2016 {
  replace rev_ipo=rev`i'/1000 if year1==`i'
  replace rev_pre1=rev`i-1'/1000 if year1-1==`i'
  replace rev_pre2=rev`i-2'/1000 if year1-2==`i'
  replace rev_pre3=rev`i-3'/1000 if year1-3==`i'
  replace rev_post1=rev`i+1'/1000 if year1+1==`i'
  replace rev_post2=rev`i+2'/1000 if year1+2==`i'
  replace rev_post3=rev`i+3'/1000 if year1+3==`i'
  }
  
// Profit (scaled)
gen profit_ipo=.
gen profit_pre1=.
gen profit_pre2=.
gen profit_pre3=.
gen profit_post1=.
gen profit_post2=.
gen profit_post3=.

forvalues i=2004/2016 {
  replace profit_ipo=profit`i'/1000 if year1==`i'
  replace profit_pre1=profit`i-1'/1000 if year1-1==`i'
  replace profit_pre2=profit`i-2'/1000 if year1-2==`i'
  replace profit_pre3=profit`i-3'/1000 if year1-3==`i'
  replace profit_post1=profit`i+1'/1000 if year1+1==`i'
  replace profit_post2=profit`i+2'/1000 if year1+2==`i'
  replace profit_post3=profit`i+3'/1000 if year1+3==`i'
  } 
  
// Profit rate (%)
gen profitrate_ipo=.
gen profitrate_pre1=.
gen profitrate_pre2=.
gen profitrate_pre3=.
gen profitrate_post1=.
gen profitrate_post2=.
gen profitrate_post3=.

forvalues i=2004/2016 {
  replace profitrate_ipo=profit`i'/(rev`i'+1)*100 if year1==`i'
  replace profitrate_pre1=profit`i-1'/(rev`i-1'+1)*100 if year1-1==`i'
  replace profitrate_pre2=profit`i-2'/(rev`i-2'+1)*100 if year1-2==`i'
  replace profitrate_pre3=profit`i-3'/(rev`i-3'+1)*100 if year1-3==`i'
  replace profitrate_post1=profit`i+1'/(rev`i+1'+1)*100 if year1+1==`i'
  replace profitrate_post2=profit`i+2'/(rev`i+2'+1)*100 if year1+2==`i'
  replace profitrate_post3=profit`i+3'/(rev`i+3'+1)*100 if year1+3==`i'
  } 
  

// Business Tax
gen taxbrate_ipo=.
gen taxbrate_pre1=.
gen taxbrate_pre2=.
gen taxbrate_pre3=.
gen taxbrate_post1=.
gen taxbrate_post2=.
gen taxbrate_post3=.

forvalues i=2004/2016 {
  replace taxbrate_ipo=taxb`i'/(rev`i'+1)*100 if year1==`i'
  replace taxbrate_pre1=taxb`i-1'/(rev`i-1'+1)*100 if year1-1==`i'
  replace taxbrate_pre2=taxb`i-2'/(rev`i-2'+1)*100 if year1-2==`i'
  replace taxbrate_pre3=taxb`i-3'/(rev`i-3'+1)*100 if year1-3==`i'
  replace taxbrate_post1=taxb`i+1'/(rev`i+1'+1)*100 if year1+1==`i'
  replace taxbrate_post2=taxb`i+2'/(rev`i+2'+1)*100 if year1+2==`i'
  replace taxbrate_post3=taxb`i+3'/(rev`i+3'+1)*100 if year1+3==`i'
  }
 

// VAT
gen taxarate_ipo=.
gen taxarate_pre1=.
gen taxarate_pre2=.
gen taxarate_pre3=.
gen taxarate_post1=.
gen taxarate_post2=.
gen taxarate_post3=.

forvalues i=2004/2016 {
  replace taxarate_ipo=taxa`i'/(rev`i'+1)*100 if year1==`i'
  replace taxarate_pre1=taxa`i-1'/(rev`i-1'+1)*100 if year1-1==`i'
  replace taxarate_pre2=taxa`i-2'/(rev`i-2'+1)*100 if year1-2==`i'
  replace taxarate_pre3=taxa`i-3'/(rev`i-3'+1)*100 if year1-3==`i'
  replace taxarate_post1=taxa`i+1'/(rev`i+1'+1)*100 if year1+1==`i'
  replace taxarate_post2=taxa`i+2'/(rev`i+2'+1)*100 if year1+2==`i'
  replace taxarate_post3=taxa`i+3'/(rev`i+3'+1)*100 if year1+3==`i'
  }

// subsidy rate
gen gsrate_ipo=.
gen gsrate_pre1=.
gen gsrate_pre2=.
gen gsrate_pre3=.
gen gsrate_post1=.
gen gsrate_post2=.
gen gsrate_post3=.

forvalues i=2007/2016 {
  replace gsrate_ipo=gs`i'/(rev`i'+1)*100 if year1==`i'
  replace gsrate_pre1=gs`i-1'/(rev`i-1'+1)*100 if year1-1==`i'
  replace gsrate_pre2=gs`i-2'/(rev`i-2'+1)*100 if year1-2==`i'
  replace gsrate_pre3=gs`i-3'/(rev`i-3'+1)*100 if year1-3==`i'
  replace gsrate_post1=gs`i+1'/(rev`i+1'+1)*100 if year1+1==`i'
  replace gsrate_post2=gs`i+2'/(rev`i+2'+1)*100 if year1+2==`i'
  replace gsrate_post3=gs`i+3'/(rev`i+3'+1)*100 if year1+3==`i'
  } 

// R&D ratio
gen rdrate_ipo=.
gen rdrate_pre1=.
gen rdrate_pre2=.
gen rdrate_pre3=.
gen rdrate_post1=.
gen rdrate_post2=.
gen rdrate_post3=.

forvalues i=2013/2016 {
  replace rdrate_ipo=inov`i'/(rev`i'+1)*100 if year1==`i'
  replace rdrate_pre1=inov`i-1'/(rev`i-1'+1)*100 if year1-1==`i'
  replace rdrate_pre2=inov`i-2'/(rev`i-2'+1)*100 if year1-2==`i'
  replace rdrate_pre3=inov`i-3'/(rev`i-3'+1)*100 if year1-3==`i'
  replace rdrate_post1=inov`i+1'/(rev`i+1'+1)*100 if year1+1==`i'
  replace rdrate_post2=inov`i+2'/(rev`i+2'+1)*100 if year1+2==`i'
  replace rdrate_post3=inov`i+3'/(rev`i+3'+1)*100 if year1+3==`i'
  }

*************************
***** Data Analysis *****
*************************

** Average post-ipo performance in 1 to 3 years
capture drop delta_*
foreach x in roa roe profit rev profitrate taxbrate taxarate gsrate rdrate {
    gen delta_`x'1=`x'_post1-`x'_pre1
    gen delta_`x'2=(`x'_post1+`x'_post2)/2-(`x'_pre1+`x'_pre2)/2
	   replace delta_`x'2 = (`x'_post1+`x'_post2)/2 - `x'_pre1 if mi(`x'_pre2)
	   replace delta_`x'2 = `x'_post1 - (`x'_pre1+`x'_pre2)/2 if mi(`x'_post2)
    gen delta_`x'3=(`x'_post1+`x'_post2+`x'_post3)/3-(`x'_pre1+`x'_pre2+`x'_pre3)/3
	   replace delta_`x'3 = (`x'_post1+`x'_post2+`x'_post3)/3-(`x'_pre1+`x'_pre2)/2 if mi(`x'_pre3)
	   replace delta_`x'3 = (`x'_post1+`x'_post2+`x'_post3)/3-`x'_pre1 if mi(`x'_pre2)
	   replace delta_`x'3=(`x'_post1+`x'_post2)/2-(`x'_pre1+`x'_pre2+`x'_pre3)/3 if mi(`x'_post3)
	   replace delta_`x'3=`x'_post1-(`x'_pre1+`x'_pre2+`x'_pre3)/3 if mi(`x'_post2)
}

***** Table 5 ******
capture drop postave_*
foreach x in roa roe profit rev profitrate taxbrate gsrate {
    gen postave_`x' = (`x'_post1+`x'_post2+`x'_post3)/3
	   replace postave_`x' = (`x'_post1+`x'_post2)/2 if mi(`x'_post3)
       replace postave_`x' = `x'_post1 if mi(`x'_post2)
}
bysort mcprom: sum(postave_*)  // mean value: column (1) and (2)

eststo clear
foreach x in postave_roa postave_roe postave_profit postave_rev postave_profitrate postave_taxbrate postave_gsrate {
    eststo: qui reg `x' mcprom
	}
    esttab, p b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(mcprom) replace  // column (3) and (4)

	
***** Table 6 *****
global cont lgfs age alratio0 npc StateOwnedShares lgcapital lgpop lggdp lggdppc gdpgrowth lgovrev lgovexp linv unerate
global fe i.year1 i.city_code i.board_code i.ind_code

foreach x in roa roe profit rev profitrate taxbrate gsrate{
    eststo clear
    eststo: qui reg delta_`x'1 mcprom $cont $fe, vce(cluster city_code)
    eststo: qui reg delta_`x'2 mcprom $cont $fe, vce(cluster city_code)
    eststo: qui reg delta_`x'3 mcprom $cont $fe, vce(cluster city_code)
    esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(mcprom) replace
}
